package cn.quickotp.mod.item.enchant;

import java.util.function.Predicate;
import java.util.stream.Stream;

import javax.lang.model.util.ElementScanner6;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
// 基础包
// 游戏包
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.scoreboard.Team;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Position;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.World;
import cn.quickotp.mod.Helper;

// 心灵搜寻
/**
 * 附魔 心灵搜寻 1 ~ 3 level = 1 受到生物伤害会标记该生物[红色] level = 2 lv1[仅限玩家] +
 * 持续标记[仅限怪物,范围16,60s] level = 3 lv2 + 持续标记[更大范围32 更持久120s]
 */
public class SoulSearchEnchant extends Enchantment {
    public SoulSearchEnchant() {
        super(Enchantment.Rarity.UNCOMMON, EnchantmentTarget.ARMOR_HEAD, new EquipmentSlot[] { EquipmentSlot.HEAD });
    }

    // 最小附魔等级要求
    @Override
    public int getMinPower(int level) {
        return 1 + (level - 1) * 10;
    }

    // 最大附魔等级要求
    @Override
    public int getMaxPower(int level) {
        return level * 10;
    }

    // 附魔等级上限
    @Override
    public int getMaxLevel() {
        return 3;
    }

    @Override
    public void onTargetDamaged(LivingEntity user, Entity target, int level) {
        // 共享一个标准时间
        int publicTime = level * 200;
        // 创建一个标准尺寸(半径)
        int r = 8 + level * 8;
        // 实例化一个发光buff给下面的实体数组使用.
        StatusEffectInstance glowingBuff = new StatusEffectInstance(StatusEffects.GLOWING, publicTime * 6, 15);
        // 可以获取的生物对象
        Helper.SearchEntityByBox(target, r, new Predicate<Entity>() {
            // 这里有遍历,不如放入这里
            public boolean test(Entity t) {
                System.out.println(t.getClass().getName());
                if (t != null && (t instanceof LivingEntity)) {
                    if (t instanceof PassiveEntity) {
                        ((LivingEntity) t).removeStatusEffectInternal(StatusEffects.GLOWING);
                    } else if (t instanceof MobEntity){
                        ((LivingEntity) t).addStatusEffect(glowingBuff);
                    } else if ( t instanceof PlayerEntity){
                        ((LivingEntity) t).addStatusEffect(glowingBuff);
                    }
                }
                return false;
            };
        });
        // 方块删除.
        BlockState blockState = user.getEntityWorld().getBlockState(user.getBlockPos());
        if (!blockState.isAir()){
            blockState.getBlock().onBreak(
                user.getEntityWorld(),
                user.getBlockPos(), 
                blockState, 
                ((PlayerEntity) user)
            );
            boolean bl = user.getEntityWorld().removeBlock(user.getBlockPos(), false);
            if(bl){
                blockState.getBlock().onBroken(user.getEntityWorld(), user.getBlockPos(), blockState);
            }
        }
        super.onTargetDamaged(user, target, level);
    }
}
